# -*- coding: utf-8 -*-
import json
import time
from smtplib import SMTP_SSL
from email.mime.text import MIMEText


def get_config():
    config = None
    with open('config.json', encoding='utf-8') as file:
        config = json.loads(file.read())
    return config


def get_parameter():
    parameter = None
    with open('parameter.json', encoding='utf-8') as file:
        parameter = json.loads(file.read())
    return parameter


def write_parameter(parameter):
    with open('parameter.json', encoding='utf-8', mode='w') as file:
        file.write(json.dumps(parameter))


def send_email(mail):
    with SMTP_SSL(host="smtp.qq.com") as smtp:
        smtp.login(user='2220677239@qq.com', password='***************')
        t = time.localtime(time.time())
        time_str = '{}-{}-{} {}:{}:{}'.format(str(t[0]).zfill(4), str(t[1]).zfill(2), str(t[2]).zfill(2),
                                              str(t[3]).zfill(2), str(t[4]).zfill(2), str(t[5]).zfill(2))
        msg = MIMEText('[时间: {}]\n{}'.format(time_str, mail), _charset="utf8")
        msg["Subject"] = "【爬虫状况】"
        msg["from"] = '2220677239@qq.com'
        msg["to"] = '2206034346@qq.com'
        smtp.sendmail(from_addr="2220677239@qq.com", to_addrs="2206034346@qq.com", msg=msg.as_string())

